package online.longzipeng.cinemasys.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import online.longzipeng.cinemasys.common.annotion.NeedPermissions;
import online.longzipeng.cinemasys.common.exception.AppException;
import online.longzipeng.cinemasys.common.result.ResponseEnum;
import online.longzipeng.cinemasys.common.result.Result;
import online.longzipeng.cinemasys.config.RedisConfig;
import online.longzipeng.cinemasys.entity.Seat;
import online.longzipeng.cinemasys.service.SeatService;
import online.longzipeng.cinemasys.vo.seat.SeatSaveVO;
import online.longzipeng.cinemasys.vo.seat.SeatUpdateVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzp
 * @since 2021-04-08
 */
@RestController
@RequestMapping("/seat")
@Api(value = "座位相关接口",description = "座位相关接口",tags = "座位相关接口")
public class SeatController {

    @Autowired
    SeatService seatService;
    @Autowired
    RedisTemplate redisTemplate;

    @PostMapping("/save")
    @NeedPermissions("seat:save")
    @ApiOperation("新增座位")
    public Result save(@Valid @RequestBody SeatSaveVO seatSaveVO){
        Seat seat = new Seat();
        BeanUtils.copyProperties(seatSaveVO,seat);
        seatService.add(seat);
        return Result.success(null,"座位添加成功！！");
    }

    @GetMapping("/delete-one")
    @NeedPermissions("seat:delete")
    @ApiOperation("删除座位")
    public Result deleteOne(@ApiParam(value = "座位id",required = true)Long id){
        try {
            seatService.removeById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(ResponseEnum.FAILED);
        }
        return Result.success(null,"座位删除成功！！");
    }

    @GetMapping("/delete")
    @NeedPermissions("seat:delete")
    @ApiOperation("删除多个座位")
    public Result deleteOne(@ApiParam(value = "座位id数组",required = true)Long[] ids){
        List<Long> idList = Arrays.stream(ids).collect(Collectors.toList());
        try {
            seatService.removeByIds(idList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(ResponseEnum.FAILED);
        }
        return Result.success(null,"座位删除成功！！");
    }

    @PostMapping("/update")
    @NeedPermissions("seat:update")
    @ApiOperation("修改座位")
    public Result update(@Valid @RequestBody SeatUpdateVO seatUpdateVO) {
        Seat seat = new Seat();
        BeanUtils.copyProperties(seatUpdateVO,seat);
        seatService.updateSeat(seat);
        return Result.success(null,"座位修改成功！！");
    }

    @GetMapping("/get-seats-by-session-id")
    @NeedPermissions("seat:delete")
    @ApiOperation("通过场次id获取影厅座位及状态")
    public Result getSeatsBySessionId(@RequestParam(value = "id",required = true) Long id) {
        // 通过场次id拼接键获取座位
        String seatsJson = redisTemplate.opsForValue().get(RedisConfig.SESSION_KEY + id).toString();
        List<Seat> seats = new ArrayList<>();
        // 如果不为空则进行转化
        if (!StringUtils.isEmpty(seatsJson)) {
            seats = JSONObject.parseArray(seatsJson, Seat.class);
        }
        return Result.success(seats);
    }
}
